package controller.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class DBDaten
 */
@WebServlet("/DBDaten")
public class DBDatenServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DBDatenServlet() {
        super();
        // TODO Auto-generated constructor stub
    }
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    public void doGet(HttpServletRequest req, HttpServletResponse res)
    	throws ServletException, IOException {

    	res.setContentType("text/html");
    	PrintWriter out = res.getWriter();

    	out.print("<html><head>");
    	out.print("</head><body>");

    	out.print("<form action=\"");
    	out.print( req.getRequestURI() );
    	out.print("\" method=\"post\">");
    	out.print("<input type=\"submit\" ");
    	out.print("value=\" \">  ");
    	out.print("Display Records</form>");

    	out.print("</body></html>");

    	out.close();
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
    public void doPost(HttpServletRequest req, HttpServletResponse res)
    	throws ServletException, IOException {

    	res.setContentType("text/html");
    	PrintWriter out = res.getWriter();

    	out.print("<html><head>");
    	out.print("</head><body>");

    	out.print("<code><pre>");
    	out.print("<font color=green>ID\tFirst ");
    	out.println("Name\tLast Name\n</font>");

    	// debugging info

    	long time1 = System.currentTimeMillis();

    	// connecting to database

    	Connection con = null;
    	Statement stmt = null;
    	ResultSet rs = null;

    	try {
    		Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    		con = DriverManager.getConnection("jdbc:odbc:odbc_exmp");

    		stmt = con.createStatement();
    		rs = stmt.executeQuery("SELECT * FROM Names");

    		// displaying records

    		while(rs.next()) {
    			out.print(rs.getObject(1).toString());
    			out.print("\t");
    			out.print(rs.getObject(2).toString());
    			out.print("\t\t");
    			out.print(rs.getObject(3).toString());
    			out.print("\n");
    		}

  
    	} catch (SQLException e) {
    		throw new ServletException("Servlet Could not display records.", e);
    	} catch (ClassNotFoundException e) {
    		throw new ServletException("JDBC Driver not found.", e);
    	} finally {
    		try {
    			if(rs != null) {
    				rs.close();
    				rs = null;
    			}
    			if(stmt != null) {
    				stmt.close();
    				stmt = null;
    			}
    			if(con != null) {
    				con.close();
    				con = null;
    			}
    		} catch (SQLException e) {}
    	}

    	// debugging info

    	long time2 = System.currentTimeMillis();

    	out.print("</pre></code>");

    	out.print("<p>Search took : ");
    	out.print( (time2 - time1) );
    	out.print(" ms.</p>");

    	out.print("<p\"><a href=\"");
    	out.print( req.getRequestURI() );
    	out.print("\">Back</a></p>");

    	out.print("</body></html>");
    	out.close();
    }
}